//
//  CWHomeCollectionViewCell.swift
//  SinaBlog
//
//  Created by ChenWei on 15/11/30.
//  Copyright © 2015年 cw. All rights reserved.
//

import UIKit
import SDWebImage

private let CWHomePictureCollectionViewCellReuseIdentifier = "CWHomePictureCollectionViewCellReuseIdentifier"
class CWHomePictureCollectionView :UICollectionView {
    var status: Status? {
        didSet {
            // 8.更新配图的size
            reloadData()
            snp_updateConstraints { (make) -> Void in
                make.size.equalTo(calculatePictureCollectionCellSize())
            }
        }
    }
    
    init() {
        super.init(frame: CGRectZero, collectionViewLayout: UICollectionViewFlowLayout())
        
        registerClass(CWHomePictureCollectionViewCell.self, forCellWithReuseIdentifier: CWHomePictureCollectionViewCellReuseIdentifier)
        
        dataSource = self
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    // MARK: - 内部方法
    /**
    计算配图cell的size
    - returns: 计算配图cell的size
    */
    func calculatePictureCollectionCellSize() -> CGSize {
        // 1.配图数组为空
        if status?.pic_URLS == nil {
            return CGSizeZero
        }
        
        // 2.配图数组不为空
        let count = status!.pic_URLS!.count ?? 0
        
        // 2.1.没有配图
        if count == 0 {
            return CGSizeZero
        }
        
        let layout = collectionViewLayout as! UICollectionViewFlowLayout
        
        // 2.2.一张配图
        if count == 1 {
            let picKey =  status!.pic_URLS?.first?.absoluteString
            let pic = SDWebImageManager.sharedManager().imageCache.imageFromDiskCacheForKey(picKey)
            
            // 设置cell的size
            layout.itemSize = pic.size
            return pic.size
        }
        
        // 2.3.四张配图
        let pictureWidth = 90;
        let pictureHeight = 90;
        let pictureMargin = 10
        // 设置cell的size
        layout.itemSize = CGSize(width: pictureWidth, height: pictureHeight)
        
        if count == 4 {
            return CGSize(width: pictureWidth * 2 + pictureMargin, height: pictureHeight * 2 + pictureMargin)
        }
        // 2.4.其它情况 (2,3,5,6,7,9张)
        let col = 3
        let row = (count + col - 1) / col
        let cellWidth = pictureWidth * col + (col - 1) * pictureMargin
        let cellHeight = pictureHeight * row + (row - 1) * pictureMargin
        return CGSize(width: cellWidth, height: cellHeight)
    }
}


// MARK: - CWHomePictureCollectionView->CWHomePictureCollectionViewDataSource的代理方法
extension CWHomePictureCollectionView: UICollectionViewDataSource {
    func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return status?.pic_URLS?.count ?? 0
    }
    
    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(CWHomePictureCollectionViewCellReuseIdentifier, forIndexPath: indexPath) as! CWHomePictureCollectionViewCell
        
        cell.imageURL = status!.pic_URLS![indexPath.item]
//        print(cell.frame.size , collectionView.frame.size,status?.pic_URLS?.count,cell.imageURL)
        
        return cell
    }
}

// MAKRK: - CWHomeCollectionViewCell
class CWHomePictureCollectionViewCell: UICollectionViewCell {
    /// 图片URL
    var imageURL: NSURL? {
        didSet {
            iv.sd_setImageWithURL(imageURL)
            NSLog(message: iv.frame.size)
        }
        
    }

    override init(frame: CGRect) {
        super.init(frame: frame)
        
        contentView.addSubview(iv)
        
        iv.snp_makeConstraints { (make) -> Void in
            make.size.equalTo(contentView)
            NSLog(message: iv.frame.size)
        }
    }
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    // MARK: - 懒加载
    private lazy var iv: UIImageView = UIImageView()
}

